package com.hyjy.music.util

import android.app.Activity
import android.os.Build
import androidx.core.content.ContextCompat
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import android.app.AppOpsManager
import android.content.Context
import android.os.Binder
import android.os.Process
import android.provider.Settings
import java.lang.Exception
import java.util.ArrayList

/**
 * 权限申请帮助类
 */
object PermissionHelper {
    @JvmStatic
    fun checkPermission(
        activity: Activity, permissions: Array<String>,
        requestCode: Int, onPermissionReady: () -> Unit
    ) {
        if (permissions.isEmpty()) {
            return
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            // Android 6.0以下不需要动态申请权限
            onPermissionReady()
            return
        }
        // Android 6.0以上动态申请权限
        val permissionList: MutableList<String> = ArrayList()
        for (permission in permissions) {
            if (ContextCompat.checkSelfPermission(
                    activity,
                    permission
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                // 未授权的权限
                permissionList.add(permission)
            }
        }
        if (permissionList.isEmpty()) {
            // 所有权限都有了
            onPermissionReady()
        } else {
            // 动态申请权限
            val permissionArray = permissionList.toTypedArray()
            ActivityCompat.requestPermissions(activity, permissionArray, requestCode)
        }
    }

    fun checkFloatPermission(context: Context): Boolean {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return true
        }
        return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            try {
                var cls = Class.forName("android.content.Context")
                val declaredField = cls.getDeclaredField("APP_OPS_SERVICE")
                declaredField.isAccessible = true
                var obj: Any? = declaredField[cls] as? String ?: return false
                val str2 = obj as String
                obj = cls.getMethod("getSystemService", String::class.java).invoke(context, str2)
                cls = Class.forName("android.app.AppOpsManager")
                val declaredField2 = cls.getDeclaredField("MODE_ALLOWED")
                declaredField2.isAccessible = true
                val checkOp =
                    cls.getMethod("checkOp", Integer.TYPE, Integer.TYPE, String::class.java)
                val result =
                    checkOp.invoke(obj, 24, Binder.getCallingUid(), context.packageName) as Int
                result == declaredField2.getInt(cls)
            } catch (e: Exception) {
                false
            }
        } else {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                val appOpsMgr = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
//                val mode = appOpsMgr.unsafeCheckOpNoThrow(
//                    "android:system_alert_window",
//                    Process.myUid(),
//                    context.packageName
//                )
//                mode == AppOpsManager.MODE_ALLOWED || mode == AppOpsManager.MODE_IGNORED
//            } else {
//                Settings.canDrawOverlays(context)
//            }

            Settings.canDrawOverlays(context)
        }
    }
}